﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPM_IN306010UV01;
using YLW.Plat.Jhfw.Model.JhfwRygl;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPM_IN306010UV01
    {
        public static string ProviderInfoQuery(GetJhfwRyglListIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //性别
            string sexCode = "";
            string sexName = "";
            switch (jsonData.sex)
            {
                case "B":
                    sexCode = "9";
                    sexName = "未说明的性别";
                    break;
                case "F":
                    sexCode = "2";
                    sexName = "女性";
                    break;
                case "M":
                    sexCode = "1";
                    sexName = "男性";
                    break;
                case "U":
                    sexCode = "0";
                    sexName = "未知的性别";
                    break;
            }

            //创建根对象
            var xmlRoot = new PRPM_IN306010UV01
            {
                ITSVersion = "XML_1.0",
                id1 = new id1 { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    telecom = new telecom(),
                    device = new device
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = receiver_name
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    telecom = new telecom(),
                    device = new device
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = sender_name
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",
                    queryByParameterPayload = new queryByParameterPayload
                    {
                        statusCode = new statusCode { code = "new" },
                        administrativeGender = new administrativeGender
                        {
                            value = new value
                            {
                                code = sexCode,
                                codeSystem = "2.16.156.10011.2.3.3.4",
                                Items = new object[]
                                {
                                    new displayName{ value=sexName }
                                }
                            }
                        },
                        dOB = new dOB
                        {
                            value = new value
                            {
                                Items = new object[]
                                {
                                    new low{ value="" },
                                    new low{ value="" }
                                }
                            }
                        },
                        providerID = new value[] {
                            new value{ root="2.16.156.10011.1.4",extension = jsonData.empid },
                            new value{ root="2.16.156.10011.1.3",extension = jsonData.pcid }
                        },
                        providerName = new providerName
                        {
                            value = new value
                            {
                                Items = new object[]
                                {
                                    new part
                                    {
                                        value=jsonData.empname
                                    }
                                }
                            }
                        },
                        extension = new extension
                        {
                            page = new page { value = new value { Text = new string[] { jsonData.page.ToString() } } },
                            percount = new percount { value = new value { Text = new string[] { jsonData.percount.ToString() } } },
                            hoscode = new hoscode { value = new value { Text = new string[] { jsonData.hoscode } } },
                            empid = new empid { value = new value { Text = new string[] { jsonData.empid } } },
                            empname = new empname { value = new value { Text = new string[] { jsonData.empname } } },
                            sex = new sex { value = new value { Text = new string[] { jsonData.sex } } },
                            pcid = new pcid { value = new value { Text = new string[] { jsonData.pcid } } },
                            birthday_low = new birthday_low { value = new value { Text = new string[] { jsonData.birthday_low } } },
                            birthday_high = new birthday_high { value = new value { Text = new string[] { jsonData.birthday_high } } }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
